<template>
  <div class="app-container">
    <!-- 添加或修改车场管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="小区名称" prop="communityName">
          <el-input v-model="form.communityName" placeholder="请输入项目名称" disabled/>
        </el-form-item>
        <el-form-item label="车场名称" prop="parkingName">
          <el-input v-model="form.parkingName" placeholder="请输入车场名称" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="车场类型" prop="parkingType">
          <el-select v-model="form.parkingType" placeholder="请选择车场类型" :disabled="disabled">
            <el-option
              v-for="item in dict.parking_type"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车场面积" prop="parkingArea">
          <el-input v-model="form.parkingArea" placeholder="请输入车场面积" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" :disabled="disabled"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getParking, addParking, updateParking} from "@/api/property/parking";

  export default {
    name: "ParkingFrom",
    dicts: ['parking_type',],
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          communityId: [
            {required: true, message: "小区名称不能为空", trigger: "blur"}
          ],
          parkingType: [
            {required: true, message: "车场类型不能为空", trigger: "change"}
          ],
          parkingName: [
            {required: true, message: "车场名称不能为空", trigger: "change"}
          ],
          orderNum: [
            {
              pattern: /^([1-9]\d*.?|0.)\d*$/,
              message: "请输入整数",
              trigger: "blur"
            }
          ],
        }
      };
    },
    created() {

    },
    methods: {
      show(option) {
        this.reset();
        this.disabled = option.type == 'VIEW'
        if (option.type == 'ADD') {
          this.open = true;
          this.title = "新增车场管理";
          this.form.communityName = option.currentNode.label
          this.form.communityId = option.currentNode.id
        } else if (option.type == 'UPDATE' || option.type == 'VIEW') {
          getParking(option.id).then(response => {
            this.form = response.data;
            if (option.type == 'VIEW') {
              this.title = "查看车场管理";
            } else {
              this.title = "修改车场管理";
            }
            this.open = true;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("ok")
      },
      // 表单重置
      reset() {
        this.form = {
          parkingId: null,
          communityId: null,
          communityName: null,
          parkingName: null,
          parkingType: null,
          parkingArea: null,
          orderNum: null,
          deptId: null,
          ancestors: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.parkingId != null) {
              updateParking(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addParking(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      }
    }
  };
</script>
